import request from '@/utils/request'

// 用户管理相关接口
export function getUsers() {
  // return request({
  //   url: '/system/users',
  //   method: 'get',
  //   params
  // })

  // 模拟数据，用于前端开发调试
  const mockList = [
    { id: 1, username: 'admin', name: '管理员', role: 'admin', status: '启用', createTime: '2023-01-01' },
    { id: 2, username: 'operator', name: '操作员', role: 'operator', status: '启用', createTime: '2023-01-01' }
  ]
  const mockTotal = mockList.length

  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        code: 20000,
        data: { total: mockTotal, items: mockList }
      })
    }, 500) // 模拟网络延迟
  })
}

export function addUser(data) {
  return request({
    url: '/system/users',
    method: 'post',
    data
  })
}

export function updateUser(id, data) {
  return request({
    url: `/system/users/${id}`,
    method: 'put',
    data
  })
}

export function deleteUser(id) {
  return request({
    url: `/system/users/${id}`,
    method: 'delete'
  })
}

// 角色管理相关接口
let mockRoleList = [
  { id: 1, name: '管理员', code: 'admin', description: '系统管理员' },
  { id: 2, name: '操作员', code: 'operator', description: '普通操作员' }
];

export function getRoles() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        code: 20000,
        data: { total: mockRoleList.length, items: mockRoleList }
      });
    }, 300);
  });
}

export function addRole(data) {
  return new Promise(resolve => {
    setTimeout(() => {
      const newItem = { ...data, id: Date.now() };
      mockRoleList.unshift(newItem);
      resolve({ code: 20000, data: newItem });
    }, 300);
  });
}

export function updateRole(id, data) {
  return new Promise(resolve => {
    setTimeout(() => {
      mockRoleList = mockRoleList.map(item => item.id === id ? { ...item, ...data } : item);
      resolve({ code: 20000, data: { ...data, id } });
    }, 300);
  });
}

export function deleteRole(id) {
  return new Promise(resolve => {
    setTimeout(() => {
      mockRoleList = mockRoleList.filter(item => item.id !== id);
      resolve({ code: 20000 });
    }, 300);
  });
}

// 权限管理相关接口
// export function getRolePermissions(roleId) {
//   return request({
//     url: `/system/roles/${roleId}/permissions`,
//     method: 'get'
//   })
// }

// export function updateRolePermissions(roleId, data) {
//   return request({
//     url: `/system/roles/${roleId}/permissions`,
//     method: 'put',
//     data
//   })
// }

export function getAllPermissions() {
  return request({
    url: '/system/permissions',
    method: 'get'
  })
}

// Mock permission data (a more comprehensive tree structure based on router files)
const mockPermissionTree = [
  {
    id: 1,
    name: '首页',
    children: []
  },
  {
    id: 2,
    name: '库存管理',
    children: [
      { id: 21, name: '库存列表' },
      { id: 22, name: '入库管理' },
      { id: 23, name: '出库管理' }
    ]
  },
  {
    id: 3,
    name: '商品管理',
    children: [
      { id: 31, name: '商品列表' },
      { id: 32, name: '商品分类' }
    ]
  },
  {
    id: 4,
    name: '销售管理',
    children: [
      { id: 41, name: '销售记录' },
      { id: 42, name: '销售统计' }
    ]
  },
  {
    id: 5,
    name: '系统管理',
    children: [
      { id: 51, name: '用户管理' },
      { id: 52, name: '角色管理' }
    ]
  },
  {
    id: 6,
    name: '系统设置',
    children: []
  },
  {
    id: 7,
    name: '普通设置',
    children: []
  }
  // Add other potential permissions if needed, e.g., specific actions
  // {
  //   id: 6,
  //   name: '数据统计',
  //   children: [
  //     { id: 61, name: '报表查看' },
  //     { id: 62, name: '数据导出' }
  //   ]
  // }
];

// Mock data to store role permissions (mapping roleId to an array of permission IDs)
let mockRolePermissions = {
  1: [1, 2, 21, 22, 23, 3, 31, 32, 4, 41, 42, 5, 51, 52, 6, 7], // Admin gets both new permissions
  2: [1, 2, 21, 22, 23, 3, 31, 32, 4, 41, 42, 7] // Operator gets General Settings
};

// Mock API to get permission tree and role's permissions
export function getRolePermissions(roleId) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        code: 20000,
        data: {
          permissionTree: mockPermissionTree,
          checkedPermissions: mockRolePermissions[roleId] || []
        }
      });
    }, 300);
  });
}

// Mock API to update role's permissions
export function updateRolePermissions(roleId, permissionIds) {
  return new Promise(resolve => {
    setTimeout(() => {
      mockRolePermissions[roleId] = permissionIds;
      resolve({ code: 20000, data: { roleId, permissionIds } });
    }, 300);
  });
} 